<template>
  <div class="app-container">
    <!-- 查询表单 -->
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="88px">
      <el-form-item label="手机号" prop="phoneNumber">
        <el-input v-model="queryParams.phoneNumber" placeholder="请输入手机号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="姓名" prop="name">
        <el-input v-model="queryParams.name" placeholder="请输入工程师姓名" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 操作按钮 -->
    <el-row :gutter="10" class="mb8">
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <!-- 工程师列表 -->
    <el-table v-loading="loading" :data="engineerList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="编号" align="center" type="index" />
      <el-table-column label="头像" align="center" prop="avatarUrl" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.avatarUrl" :width="50" :height="50" />
        </template>
      </el-table-column>
      <el-table-column label="手机号" align="center" prop="phoneNumber" />
      <el-table-column label="员工编号" align="center" prop="employeeId" />
      <el-table-column label="姓名" align="center" prop="name" />
      <el-table-column label="性别" align="center" prop="gender">
        <template slot-scope="scope">
          {{ scope.row.gender === 0 ? '男' : '女' }}
        </template>
      </el-table-column>
      <el-table-column label="身份证号" align="center" prop="idCard" />
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <el-switch v-model="scope.row.status" active-color="#ff4949" inactive-color="#13ce66" :active-value="1"
            :inactive-value="0" @change="handleChangeStatus(scope.row)">
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-view" @click="handleDetail(scope.row)"
            v-hasPermi="['engineer:detail']">详情</el-button>
          <el-button size="mini" type="text" icon="el-icon-check" @click="handleAudit(scope.row)"
            v-if="scope.row.authenticationStatus === 1">审核</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 工程师详情对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-row :gutter="20">
        <el-col :span="24">
          <el-form label-width="100px">
            <el-form-item label="手机号">
              {{ form.phoneNumber }}
            </el-form-item>
            <el-form-item label="员工编号">
              {{ form.employeeId }}
            </el-form-item>
            <el-form-item label="姓名">
              {{ form.name }}
            </el-form-item>
            <el-form-item label="性别">
              {{ form.gender === 0 ? '男' : '女' }}
            </el-form-item>
            <el-form-item label="身份证号">
              {{ form.idCard }}
            </el-form-item>
            <el-form-item label="头像">
              <image-preview :src="form.avatarUrl" :width="50" :height="50" />
            </el-form-item>
            <el-form-item label="真实姓名认证">
              <div v-for="(image, index) in form.realnameAuth" :key="index">
                <img :src="image" alt="认证图片" style="max-width: 100%; max-height: 100%;">
              </div>
            </el-form-item>

            <el-form-item label="证书">
              <div v-for="(image, index) in form.certificate" :key="index">
                <img :src="image" alt="评价图片" style="max-width: 100%; max-height: 100%;">
              </div>
            </el-form-item>
            <el-form-item label="押金">
              {{ form.deposit }}
            </el-form-item>
            <el-form-item label="余额">
              {{ form.balance }}
            </el-form-item>
            <el-form-item label="状态">
              {{ form.status === 0 ? '正常' : '禁用' }}
            </el-form-item>
            <el-form-item label="认证状态">
              {{ getAuthenticationStatusText(form.authenticationStatus) }}
            </el-form-item>
          </el-form>
        </el-col>
      </el-row>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">关 闭</el-button>
        <el-button type="primary" @click="handleApprove" v-if="showAuditButtons">通 过</el-button>
        <el-button type="danger" @click="handleReject" v-if="showAuditButtons">驳 回</el-button>
      </div>
    </el-dialog>


  </div>
</template>

<script>
import { listEngineer, getEngineer, updateEngineer } from "@/api/user/engineer";

export default {
  name: "Engineer",
  data() {
    return {
      showAuditButtons:false,
      loading: true,
      ids: [],
      single: true,
      multiple: true,
      showSearch: true,
      total: 0,
      engineerList: [],
      title: "",
      open: false,
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        phoneNumber: null,
        name: null,
      },
      form: {},
      rules: {
        phoneNumber: [
          { required: true, message: "手机号不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    handleAudit(row) {
      this.reset();
      const id = row.id;
      getEngineer(id).then(response => {
        let data = response.data;

        // 将 realnameAuth 转换为数组
        if (data.realnameAuth && typeof data.realnameAuth === 'string') {
          data.realnameAuth = data.realnameAuth.split(',').map(url => url.trim());
        } else {
          data.realnameAuth = [];
        }

        // 将 certificate 转换为数组
        if (data.certificate && typeof data.certificate === 'string') {
          data.certificate = data.certificate.split(',').map(url => url.trim());
        } else {
          data.certificate = [];
        }

        this.form = data;
        this.open = true;
        this.title = "工程师审核";
        this.showAuditButtons = true; // 显示审核按钮
      });
    },
    handleApprove() {
      const params = {
        id: this.form.id,
        authenticationStatus: 2
      };
      updateEngineer(params).then(() => {
        this.$modal.msgSuccess('审核通过');
        this.cancel();
        this.getList();
      }).catch(() => {
        this.$modal.msgError('审核失败');
      });
    },
    handleReject() {
      const params = {
        id: this.form.id,
        authenticationStatus: 3
      };
      updateEngineer(params).then(() => {
        this.$modal.msgSuccess('审核驳回');
        this.cancel();
        this.getList();
      }).catch(() => {
        this.$modal.msgError('审核失败');
      });
    },
    //获取认证状态
    getAuthenticationStatusText(status) {
      switch (status) {
        case 0:
          return '未认证';
        case 1:
          return '待审核';
        case 2:
          return '认证成功';
        case 3:
          return '已驳回';
        default:
          return '未知状态';
      }
    },
    getList() {
      this.loading = true;
      listEngineer(this.queryParams).then(response => {
        this.engineerList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    cancel() {
      this.open = false;
      this.reset();
    },
    reset() {
      this.form = {
        id: null,
        phoneNumber: null,
        employeeId: null,
        name: null,
        gender: null,
        idCard: null,
        avatarUrl: null,
        realnameAuth: [],
        certificate: [],
        deposit: null,
        balance: null,
        status: null,
        authenticationStatus: null,
      };
      this.resetForm("form");
    },
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    handleDetail(row) {
      this.reset();
      const id = row.id;
      getEngineer(id).then(response => {
        let data = response.data;

        // 将 realnameAuth 转换为数组
        if (data.realnameAuth && typeof data.realnameAuth === 'string') {
          data.realnameAuth = data.realnameAuth.split(',').map(url => url.trim());
        } else {
          data.realnameAuth = [];
        }

        // 将 certificate 转换为数组
        if (data.certificate && typeof data.certificate === 'string') {
          data.certificate = data.certificate.split(',').map(url => url.trim());
        } else {
          data.certificate = [];
        }

        this.form = data;
        this.open = true;
        this.title = "工程师详情";
        this.showAuditButtons = false; // 不显示审核按钮
      });
    },
    handleChangeStatus(row) {
      const params = {
        id: row.id,
        status: row.status
      };
      updateEngineer(params).then(() => {
        this.$modal.msgSuccess('状态更新成功');
      }).catch(() => {
        row.status = row.status === 0 ? 1 : 0;
      });
    }
  }
};
</script>

<style scoped>
.app-container {
  padding: 20px;
}

.el-form-item {
  margin-bottom: 15px;
}

.el-table {
  margin-top: 20px;
}
</style>